var rgxdate = /^[0-9]{4}\-(0?[1-9]|1[0-2])\-(0?[1-9]|[1-2][0-9]|3[01])$/;
var rgxnumber = /^[0-9]+$/;

function eid(id) {
    return document.getElementById(id);
}

function setCookie(c_name, value, exdays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays === null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function httpGet(value) {
    var tmp = (window.location + "").split("?");
    if (tmp.length > 1) {
        var arr = tmp[1].split("&");
        for (var i in arr) {
            var splits = arr[i].split("=");
            if (splits[0] === value)
                return splits[1];
        }
    }
    return false;
}

function getXmlHttp() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

function xhr(data, page) {
    xmlhttp = getXmlHttp();
    xmlhttp.open("POST", page, (arguments.length === 3));
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    if (arguments.length === 3) {
        xmlhttp.onreadystatechange = arguments[2];
    }
    xmlhttp.send(data);
    return xmlhttp;
}

function validRegex(regex, input) {
    if (!(regex.test(input.value))) {
        input.parentNode.setAttribute("class", "error");
    } else {
        input.parentNode.setAttribute("class", "");
    }
}

function validNumber(input) {
    validRegex(rgxnumber, input);
}

function validDate(input) {
    validRegex(rgxdate, input);
}

function processForm(type) {
    processFormById(type, "form");
}

function processFormById(type, id) {
    eid("tmp").value = id + "id";
    var form = eid(id);
    var vars = "";
    var submit = true;
    var fields = form.getElementsByTagName("input");
    var radio = null;
    var value;
    for (var i = 1; i < fields.length; i++) {
        if (fields[i].getAttribute("name") !== "skip") {
            value = fields[i].value;
            if (fields[i].getAttribute("name") !== "mail" && fields[i].value === "") {
                fields[i].parentNode.setAttribute("class", "error");
                submit = false;
            }
            if (fields[i].getAttribute("type") === "radio") {
                radio = fields[i].getAttribute("name");
                var checked = false;
                while (i < fields.length && radio === fields[i].getAttribute("name")) {
                    if (fields[i].checked) {
                        checked = true;
                        value = fields[i].value;
                    }
                    i++;
                }
                i--;
                if (!checked) {
                    fields[i].parentNode.setAttribute("class", "error");
                    submit = false;
                }
            }
            if (fields[i].id === "preclave") {
                if (fields[i].value !== fields[i + 1].value) {
                    fields[i].parentNode.setAttribute("class", "error");
                    i++;
                    fields[i].parentNode.setAttribute("class", "error");
                    submit = false;
                } else {
                    i++;
                }
            }
            vars += fields[i].getAttribute("name") + "=" + escape(value) + "&";
        }
    }
    var txtfields = form.getElementsByTagName("textarea");
    for (var i = 0; i < txtfields.length; i++) {
        if (txtfields[i].id !== "comments" && txtfields[i].value === "") {
            txtfields[i].parentNode.setAttribute("class", "error");
            submit = false;
        }
        vars += txtfields[i].getAttribute("name") + "=" + escape(txtfields[i].value) + "&";
    }
    var selects = form.getElementsByTagName("select");
    for (var i = 0; i < selects.length; i++) {
        if (selects[i].value === "") {
            selects[i].parentNode.setAttribute("class", "error");
            submit = false;
        }
        vars += selects[i].getAttribute("name") + "=" + escape(selects[i].value) + "&";
    }
    if (submit) {
        vars = vars.substr(0, vars.length - 1);
        eid("msg").innerHTML = "Guardando informacion...";
        eid("msg").setAttribute("class", "msg");
        xhr("id=" + eid(eid("tmp").value).value + "&" + vars, "store.php?type=" + type, function()
        {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
            {
                setCookie("msg", "La informacion ha sido almacenada correctamente.");
                if (!httpGet("id")) {
                    window.location = window.location + "?id=" + xmlhttp.responseText;
                } else {
                    location.reload();
                }
            }
            if (xmlhttp.readyState === 4 && xmlhttp.status === 403)
            {
                location.reload();
            }
            if (xmlhttp.readyState === 4 && (xmlhttp.status === 500 || xmlhttp.status === 404))
            {
                eid("msg").innerHTML = xmlhttp.responseText;
                eid("msg").setAttribute("class", "error");
            }
        });
    } else {
        eid("msg").innerHTML = "Por facvor complete los campos correctamente.";
        eid("msg").setAttribute("class", "error");
    }
}

function delItem(id, type, rown) {
    eid("tmp").value = rown.parentNode.parentNode.rowIndex;
    eid("msg").innerHTML = "Eliminando...";
    eid("msg").setAttribute("class", "msg");
    xhr("id=" + id + "&type=" + type, "delete.php", function()
    {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            eid("msg").innerHTML = "Eliminado Correctamente";
            eid("list").deleteRow(eid("tmp").value);
        }
        if (xmlhttp.readyState === 4 && xmlhttp.status === 403)
        {
            location.reload();
        }
        if (xmlhttp.readyState === 4 && (xmlhttp.status === 500 || xmlhttp.status === 404))
        {
            eid("msg").innerHTML = xmlhttp.responseText;
            eid("msg").setAttribute("class", "error");
        }
    });
}

function selectCliente() {
    var slt = eid('cliente_id');
    var val = eid('id_cliente').value;
    for (var i = 0; i < slt.options.length; ++i) {
        if (parseInt(slt.options[i].value) === parseInt(val.substr(1,val.length))) {
            slt.selectedIndex = i;
            break;
        }
    }
}

function addLinea(){
    var table=document.getElementById("myTable");
var row=table.insertRow(0);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
cell1.innerHTML="New";
cell2.innerHTML="New";
}

